/*
	Copyright (c) 2010 Canadensys
*/
package net.canadensys.dataportal.vascan;
import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import net.canadensys.dataportal.utils.HibernateUtil;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

@Entity
@Table(name="distribution")
@Cache(region="Distribution", usage = CacheConcurrencyStrategy.READ_WRITE)
public class Distribution implements Serializable{
	private static final long serialVersionUID = 215525539452666707L;

	private int					id;
	private Taxon				taxon;
	private Region				region;
	private DistributionStatus	distributionStatus;
	private ExcludedCode		excludedcode;
	private Reference			reference;
	
	@Transient
	private String				error;
	
	@Transient
	/**
	 * @return the error
	 */
	public String getError() {
		return error;
	}

	/**
	 * @param error the error to set
	 */
	public void setError(String error) {
		this.error = error;
	}

	/**
	 * @return the id
	 */
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	public int getId() {
		return id;
	}
	
	/**
	 * @param distributionid the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	
	
	/**
	 * @return the taxon
	 */
	@ManyToOne
	@JoinColumn(name="taxonid")
	public Taxon getTaxon() {
		return taxon;
	}
	
	/**
	 * @param taxon 
	 */
	public void setTaxon(Taxon taxon) {
		this.taxon = taxon;
	}
	
	/**
	 * @return the region
	 */
	@ManyToOne
	@JoinColumn(name="regionid")
	public Region getRegion() {
		return region;
	}
	
	/**
	 * @param province the region to set
	 */
	public void setRegion(Region region) {
		this.region = region;
	}
	
	/**
	 * @return the distributionStatus
	 */
	@ManyToOne
	@JoinColumn(name="distributionstatusid")
	public DistributionStatus getDistributionStatus() {
		return distributionStatus;
	}
	
	/**
	 * @param distributionStatus the distributionStatus to set
	 */
	public void setDistributionStatus(DistributionStatus distributionStatus) {
		this.distributionStatus = distributionStatus;
	}
	
	/**
	 * @return the excludedcode
	 */
	@ManyToOne
	@JoinColumn(name="excludedcodeid")
	public ExcludedCode getExcludedcode() {
		return excludedcode;
	}
	
	/**
	 * @param excludedcode the excludedcode to set
	 */
	public void setExcludedcode(ExcludedCode excludedcode) {
		this.excludedcode = excludedcode;
	}

	/**
	 * @return the reference
	 */
	@ManyToOne
	@JoinColumn(name="referenceid")
	public Reference getReference() {
		return reference;
	}

	/**
	 * @param reference the reference to set
	 */
	public void setReference(Reference reference) {
		this.reference = reference;
	}	
	
	/**
	 * 
	 */
	public boolean save(){
		boolean success = false;
		org.hibernate.Session hibernateSession = HibernateUtil.getSessionFactory().getCurrentSession();
		hibernateSession.beginTransaction();
		try{
			hibernateSession.saveOrUpdate(this);
			success = true;
		}
		catch(Exception e){
			this.error = e.getMessage();
		}
		return success;
	}
	
	/**
	 * 
	 * @return
	 */
	public boolean delete(){
		boolean success = false;
		org.hibernate.Session hibernateSession = HibernateUtil.getSessionFactory().getCurrentSession();
		hibernateSession.beginTransaction();
		try{
			hibernateSession.delete(this);
			success = true;
		}
		catch(Exception e){
			this.error = e.getMessage();
		}
		return success;
	}
	
	@Override
	/**
	 * toString is mainly used in the admin backend, where a basic string representation
	 * is dumped to the logfile, in a before/after fashion (changed from / changed to) 
	 */
	public String toString() {
		String delimiter = " ";
		//String newline = "\n";
		StringBuffer distribution = new StringBuffer("");
		distribution.append(this.id).append(delimiter);
		distribution.append(this.taxon.getLookup().getCalname()).append(delimiter);
		distribution.append(this.getDistributionStatus().getDistributionstatus()).append(delimiter);
		distribution.append(this.getExcludedcode().getExcludedcode()).append(delimiter);
		distribution.append(this.getReference().getReferenceshort());
		return distribution.toString();
	}	
}